﻿using System;
using System.Windows.Forms;

namespace YArchitech.Controls
{
	public class HYGridViewNumbericEditingControl : YJKTextBox, IDataGridViewEditingControl
	{
		public HYGridViewNumbericEditingControl()
		{
			base.TabStop = false;
			base.IsNumber = InputSelection.number;
		}

		protected override void OnTextChanged(EventArgs e)
		{
			base.OnTextChanged(e);
			double value = 0.0;
			double.TryParse(this.Text, out value);
			if (base.MostValue.Test(value))
			{
				this.NotifyDataGridViewOfValueChange();
				return;
			}
			this.Text = this.oldText;
		}

		private void NotifyDataGridViewOfValueChange()
		{
			this.valueChanged = true;
			this.dataGridView.NotifyCurrentCellDirty(true);
			this.oldText = this.Text;
		}

		private static HorizontalAlignment translateAlignment(DataGridViewContentAlignment align)
		{
			if (align <= DataGridViewContentAlignment.MiddleCenter)
			{
				switch (align)
				{
				case DataGridViewContentAlignment.TopLeft:
					break;
				case DataGridViewContentAlignment.TopCenter:
					return HorizontalAlignment.Center;
				case (DataGridViewContentAlignment)3:
					return HorizontalAlignment.Left;
				case DataGridViewContentAlignment.TopRight:
					return HorizontalAlignment.Right;
				default:
					if (align != DataGridViewContentAlignment.MiddleLeft)
					{
						if (align != DataGridViewContentAlignment.MiddleCenter)
						{
							return HorizontalAlignment.Left;
						}
						return HorizontalAlignment.Center;
					}
					break;
				}
			}
			else if (align <= DataGridViewContentAlignment.BottomLeft)
			{
				if (align == DataGridViewContentAlignment.MiddleRight)
				{
					return HorizontalAlignment.Right;
				}
				if (align != DataGridViewContentAlignment.BottomLeft)
				{
					return HorizontalAlignment.Left;
				}
			}
			else
			{
				if (align == DataGridViewContentAlignment.BottomCenter)
				{
					return HorizontalAlignment.Center;
				}
				if (align != DataGridViewContentAlignment.BottomRight)
				{
					return HorizontalAlignment.Left;
				}
				return HorizontalAlignment.Right;
			}
			return HorizontalAlignment.Left;
		}

		public Cursor EditingPanelCursor
		{
			get
			{
				return Cursors.IBeam;
			}
		}

		public DataGridView EditingControlDataGridView
		{
			get
			{
				return this.dataGridView;
			}
			set
			{
				this.dataGridView = value;
			}
		}

		public object EditingControlFormattedValue
		{
			get
			{
				return this.Text;
			}
			set
			{
				this.Text = value.ToString();
				this.NotifyDataGridViewOfValueChange();
			}
		}

		public virtual bool RepositionEditingControlOnValueChange
		{
			get
			{
				return false;
			}
		}

		public int EditingControlRowIndex
		{
			get
			{
				return this.rowIndex;
			}
			set
			{
				this.rowIndex = value;
			}
		}

		public bool EditingControlValueChanged
		{
			get
			{
				return this.valueChanged;
			}
			set
			{
				this.valueChanged = value;
			}
		}

		public virtual object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
		{
			return this.Text;
		}

		public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
		{
			return !dataGridViewWantsInputKey;
		}

		public void PrepareEditingControlForEdit(bool selectAll)
		{
			if (selectAll)
			{
				base.SelectAll();
				return;
			}
			base.SelectionStart = this.ToString().Length;
		}

		public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
		{
			this.Font = dataGridViewCellStyle.Font;
			this.ForeColor = dataGridViewCellStyle.ForeColor;
			this.BackColor = dataGridViewCellStyle.BackColor;
			base.TextAlign = HYGridViewNumbericEditingControl.translateAlignment(dataGridViewCellStyle.Alignment);
		}

		protected int rowIndex;

		protected DataGridView dataGridView;

		protected bool valueChanged;

		private string oldText = "0";
	}
}
